#!/bin/bash

# backup dl folder of buidroot

source ./envsetup

#set -v

usage() {
        echo "Usage:"
        echo " ./backup_brdl.sh -c"
        echo "  -c: compare/diff source and destination"
}

while getopts ":c" opt
do
        case $opt in
        h)
                O_HELP=y
                ;;
        c)
                O_COMPARE=y
                ;;
        ?)
                echo "there is unrecognized parameter."
                usage
                exit 1
                ;;
    esac
done

DEV=sdb1
UDISK=$WS/u-disk

PATH_SOURCE=$WS/test-buildroot/buildroot/dl/
PATH_DESTINATION=$UDISK/buildroot-dl/

if [ "$O_HELP" = "y" ]; then
	usage
	exit 0
fi

sudo umount $UDISK
sudo mount /dev/$DEV $UDISK

if [ "$O_COMPARE" == "y" ]; then

	echo "Starting compare files between destination and source ......"	
	rsync --dry-run --recursive --delete --out-format="%n" $PATH_DESTINATION $PATH_SOURCE
	echo "Comparing is completed!"

	sudo umount $UDISK
	exit 0

fi

echo "Starting sync files from source to destination ......"
rsync -aPv $PATH_SOURCE $PATH_DESTINATION
echo "Sync is completed!"

sudo umount $UDISK
